package com.morgankelly.action;

import java.util.ArrayList;
import java.util.List;

import com.morgankelly.po.Blog;
import com.morgankelly.util.Constants;


@SuppressWarnings("serial")
public class BlogAction extends BaseAction {
    
	public String input() {

		initBlogForm();
		return INPUT;
	}
	
	public String add() {

		Blog blog = new Blog();
		blog.setTitle(title);
		blog.setContent(content);
		
		blog.setAccountID(accountID);
		blog.setCategoryID(categoryID);

		getBlogService().addBlog(blog);
	    
		initBlogForm();
		return ADD;
	}
	
	public String updateInput() {
		
		Blog blog;

	    //BlogDAO.update(title, content, accountID, categoryID);
		List<Object> args = new ArrayList<Object>();
		if (blogID == null || blogID == 0) {
            args.add(Blog.class.getSimpleName());
            addActionError(getText(Constants.ERROR_ID_NOT_CHOSEN, args));
            return FAILED;
		} else {
			blog = getBlogService().getBlogByID(blogID);
			setTitle(blog.getTitle());
			setContent(blog.getContent());
			setAccountID(blog.getAccountKey().getId());
			blog.setCategoryID(blog.getCategoryKey().getId());
		}
	    
		initBlogForm();
		return UPDATE_INPUT;
	}
	
	
	public String update() {

		getBlogService().updateBlog(blogID, title, content, accountID, categoryID);
	    
		initBlogForm();
		return ADD;
	}
	
	public String delete() {
		getBlogService().deleteBlogByID(blogID);
		
		initBlogForm();
		return DELETE;
	}
	
	private void initBlogForm() {
		setBlogVOList(initBlogVOBySearchKey(""));

		setAccountList(getAuthService().getAccountList());
		setCategoryList(getBlogService().getCategoryList());
		
		setTagList(getBlogService().getTagList());
		
		initSideBar();
	}
	
	private String title;
    private String content;

    private Long accountID;
    private Long categoryID;

    private Long blogID;
    
	public Long getBlogID() {
		return blogID;
	}

	public void setBlogID(Long blogID) {
		this.blogID = blogID;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Long getAccountID() {
		return accountID;
	}

	public void setAccountID(Long accountID) {
		this.accountID = accountID;
	}

	public Long getCategoryID() {
		return categoryID;
	}

	public void setCategoryID(Long categoryID) {
		this.categoryID = categoryID;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

}
